<?php
/**
 * Created by PhpStorm.
 * User: haobin
 * Date: 2016/10/13
 * Time: 10:16
 * 自动挂机列表
 */
session_start();
require('../conn.php');
require('../functions.php');
require('admin.php');
//分页查询操作
$page = $_GET['page'];
if (is_numeric($page) == false) {
    $page = 1;
    $page_1 = 1;
    $min = 0;
    $page_2 = $page + 1;
} else {
    if ($page > 1) {
        $page_2 = $page + 1;
        $min = ($page - 1) * 35;
        $page_1 = $page - 1;
    } else {
        $page_1 = 1;
        $min = 0;
        $page_2 = 2;
    }
}
$max = 35;
$sql = "select * from `news`  WHERE `top` = 2 order by `id` asc limit {$min},{$max}";

//禁言用户
if ($_GET['action'] == 'ban') {
    $uid = $_GET['uid'];
    $day = date("Y-m-d", time());
    $res = $mysql->execute("update `user` set `ban` = 1,`ban_day` = '{$day}' WHERE `id` = {$uid}");
    if ($res) {
        echo "<script>alert('禁言成功')</script>";
        echo "<script>location.href='./wait_article.php'</script>";
    }
}

//删除文章
if ($_GET['action'] == 'del') {
    $id = $_GET['aid'];
    $uid = $_GET['uid'];
    $res = $mysql->execute("delete from `news` WHERE `id` = {$id}");
    if ($res) {
        echo "<script>alert('删除成功')</script>";
        echo "<script>location.href='./wait_article.php'</script>";
    }
}
//通过审核
if ($_GET['top'] == 1) {
    $id = $_GET['aid'];
    $uid = $_GET['uid'];
    $day = date("Y-m-d", time());
    $res = $mysql->execute("update `news` set `top`=1 WHERE `id`={$id}");
    if ($res) {
        //通过审核，在score表里面增加发布记录
        $publish = $mysql->query("select * from `score` WHERE `uid`={$uid} and `day`='{$day}'");
        //如果已经获得了90分了，就不在给积分
        if ($publish[0]['publish'] >= 90) {
            echo "<script>alert('通过审核')</script>";
            echo "<script>location.href='./wait_article.php'</script>";
        } else {
            $res = $mysql->execute("update `score` set `publish`= `publish`+30 WHERE `uid`={$uid} AND `day`='{$day}'");
            //发表成功，user增加收入30
            $res = $mysql->execute("update `user` set `total`=`total`+30 WHERE `id`={$uid}");
            $income = [
                'uid' => $uid,
                'content' => '发表文章',
                'score' => 30,
                'type' => 0,
                'day' => date("Y-m-d h:i:s", time()),
            ];
            $incomeSql = "insert into `score_detail`" . arr2s($income);
            $res = $mysql->execute($incomeSql);
            if ($res) {
                echo "<script>alert('通过审核')</script>";
                echo "<script>location.href='./wait_article.php'</script>";
            }
        }
    }
}

//搜索
if ($_GET['do'] == 'search') {
    $search_title = guolv($_GET['search_title']);
    $sql = "select * from `news` where  `content` LIKE  '%{$search_title}%' AND `top` = 0 order by `id` asc limit 35";
}
$rows = $mysql->query($sql);


//定时发布一篇
$new_time_id = $rows[0]['id'];
$userid = $rows[0]['uid'];
$day = date("Y-m-d", time());
$time = date("H-i-s", time());
$re_time = $mysql->execute("update news set top=1,day='$day',time='$time' where id=$new_time_id");
$publish = $mysql->query("select * from `score` WHERE `uid`={$userid} and `day`='{$day}'");
//没有登录就自动发布，创建一条score数据
if(!$publish){
    $mysql->execute("insert into `score` (`id`,`day`) VALUES ({$userid},'{$day}')");
}
//如果已经获得了90分了，就不在给积分
if ($publish[0]['publish'] >= 90) {
    echo "<script>location.href='./wait_article.php'</script>";
} else {
    $res = $mysql->execute("update `score` set `publish`= `publish`+30 WHERE `uid`={$userid} AND `day`='{$day}'");
//发表成功，user增加收入30,总收入+30
    if ($res) {
        $res = $mysql->execute("update `user` set `total`=`total`+30,`score`=`score`+30 WHERE `id`={$userid}");
    }
//jhi_user_authority中publish
    $res = $mysql->execute("update `jhi_user_authority` set `publish`=`publish`+30 WHERE `user_id`={$userid}");
    $income = [
        'uid' => $userid,
        'content' => '发表文章',
        'score' => 30,
        'type' => 0,
        'day' => date("Y-m-d h:i:s", time()),
    ];
    $incomeSql = "insert into `score_detail`" . arr2s($income);
    $res = $mysql->execute($incomeSql);
}
if ($res) {
//	echo "<script>alert('定时发布成功id:'+$new_time_id)</script>";
    $msg = "自动发布已成功id=" . $new_time_id;
} else {
    $msg = "无数据发布失败！";
}
?>

<?php include('head.php') ?>
<div class="container-fluid">
    <div class="row-fluid">
        <?php
        include('left.php'); ?>
        <div class="span9">
            <h1 class="page-title">
                待发布列表
            </h1>

            <p style="font-size: 25px; color:red"><?php echo "$msg" ?></p>

            <div class="well">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <div class="well">
                            <form class="navbar-search pull-left" action="" method="get">
                                <input type="hidden" name="do" value="search">
                                <input type="text" class="search-query" placeholder="搜索文章标题+回车" name="search_title">
                            </form>

                            <table class="table">
                                <form class="navbar-search pull-left" action="article.php" method="post">
                                    <thead>
                                    <tr>
                                        <th style="width: 8%;">用户id</th>
                                        <th style="width: 9%;">用户名</th>
                                        <th style="width: 8%;">发布日期</th>
                                        <th style="width: 50%;">发表内容</th>
                                        <th style="width: 25%;">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                    foreach ($rows as $row) {
                                        if ($row['img']) {
                                            $content = "<span style='color: red'>图</span>" . $row['content'];
                                        } else {
                                            $content = $row['content'];
                                        }
                                        $btn = "
		  <a class=\"btn\" href=\"dynamic_detail.php?aid={$row['id']}\" target=\"_blank\">查看</a>
			<a class=\"btn\" href=\"javascript:if(confirm('确认删除？')){window.location.href = '?aid={$row['id']}&uid={$row['uid']}&action=del';}\" style=\"color:red\">删除</a>
			<a class=\"btn\" href=\"javascript:if(confirm('确认禁言？')){window.location.href = '?uid={$row['uid']}&action=ban';}\" style=\"\">禁言</a>
			<a class=\"btn\" href=\"javascript:if(confirm('确认发布？')){window.location.href = '?aid={$row['id']}&top=1&uid={$row['uid']}';}\" style=\"\">发布</a>
			";
                                        $user = $mysql->query("select * from `user` WHERE `id` = {$row['uid']}");
                                        $username = $user[0]['name'];
                                        print <<<table
        <tr>
		  <td>{$row['uid']}</td>
		  <td>{$username}</td>
          <td>{$row['day']}</td>
          <td><a href="dynamic_detail.php?aid={$row['id']}" target="_blank">{$content}</a></td>
		  <td>{$btn}</td>
        </tr>
table;
                                    } ?>
                                    </tbody>
                            </table>
                        </div>
                        </form>
                    </div>
                    <div class="btn-group pull-right">
                        <h4>第<?php
                            if ($_GET['page']) {
                                echo $_GET['page'];
                            } else {
                                echo 1;
                            }
                            ?>页</h4>
                    </div>
                    <div class="btn-group pull-right">
                        <a href="review.php" class="btn" style="margin-left:10px;">首页</a></button>
                        <a href="review.php?page" =<?php echo $page_1 ?>" class="btn"
                        style="margin-left:5px;">上一页</a>
                        <a href="review.php?page=<?php echo $page_2 ?>" class="btn"
                           style="margin-left:5px;">下一页</a>
                    </div>
                    <div class="tab-pane fade" id="profile">
                    </div>
                </div>

            </div>

        </div>
    </div>

</div>

<script src="lib/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript">
    function re_load_nowpage() {
        window.location.reload();
    }
    var intervalTime = 8 * 60 * 1000;
    //判断是否在夜间,小于八点则二十分钟发布一次
    var today = new Date()
    var h = today.getHours()
    if (h < 8) {
        intervalTime = 20 * 60 * 1000;
    } else {
        intervalTime = 8 * 60 * 1000;
    }
    console.log(intervalTime / 1000);
    setTimeout('re_load_nowpage()', intervalTime);
</script>
</body>
</html>